package com.sdruixinggroup.mondayb2b.ui.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.sdruixinggroup.mondayb2b.API.API;
import com.sdruixinggroup.mondayb2b.R;
import com.sdruixinggroup.mondayb2b.adapter.QuickAdapter;
import com.sdruixinggroup.mondayb2b.adapter.SystemMessageAdapter;
import com.sdruixinggroup.mondayb2b.interfaces.ObjectCallBack;
import com.sdruixinggroup.mondayb2b.models.Message;
import com.sdruixinggroup.mondayb2b.models.MessagetBeen;
import com.sdruixinggroup.mondayb2b.models.ModelBeen;
import com.sdruixinggroup.mondayb2b.ui.Activities.SystemInforActivity;
import com.sdruixinggroup.mondayb2b.utils.UserInfoUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.leefeng.lfrecyclerview.LFRecyclerView;
import okhttp3.Call;

/**
 * Created by 00029935 on 2017/11/18.
 */

public class SystemMessageFragment extends BaseFragment {
    @BindView(R.id.ib_back)
    ImageView ibBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    LFRecyclerView recyclerView;
    @BindView(R.id.loading)
    ProgressBar loading;
    private SystemMessageAdapter adapter;
    private List<MessagetBeen.MessagesBean.DataBean> data;
    private Message.CategoryBean categoryBean;
    private int page = 1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        categoryBean = (Message.CategoryBean)getActivity().getIntent().getSerializableExtra("object");
    }

    @Override
    protected int getInflateLayout() {
        return R.layout.common_fragment_layout;
    }

    @Override
    protected void setUpView(View view) {
        tvTitle.setText("系统消息");

        adapter = new SystemMessageAdapter(getContext());
        data = new ArrayList<>();

        adapter.setData(data);

        recyclerView.setAdapter(adapter);
        recyclerView.setLoadMore(true);
        recyclerView.setRefresh(true);
        recyclerView.setLFRecyclerViewListener(new LFRecyclerView.LFRecyclerViewListener(){
            @Override
            public void onRefresh() {
                queryData(true);
            }

            @Override
            public void onLoadMore() {
                queryData(false);
            }
        });

        adapter.setOnItemClickListener(new QuickAdapter.OnItemClickListener<MessagetBeen.MessagesBean.DataBean>(){
            @Override
            public void OnItemClick(MessagetBeen.MessagesBean.DataBean data, int postion) {
                Intent intent = new Intent(getContext(),SystemInforActivity.class);
                intent.putExtra("object",data);
                startActivity(intent);
                read(data.getMessage_id());
            }
        });
        queryData(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick(R.id.ib_back)
    public void onClick() {
        getActivity().finish();
    }

    private void queryData(final Boolean isReflush){

        if(isFirstIn ){
            isFirstIn = false;
            loading.setVisibility(View.VISIBLE);
        }

        if(isReflush){
            page = 0;
            data.clear();
        }
        String url = API.MESSAGES+"page="+page+"&page_size=10"+"&category_id="+categoryBean.getMessage_category_id()+"&access_token="+ UserInfoUtil.getInfoToken(getContext());
        HashMap<String, String> heades = new HashMap<>();
        heades.put("Accept", "*/*");
        TypeToken<MessagetBeen> typeToken = new TypeToken<MessagetBeen>() {
        };

        OkHttpUtils.get().url(url).headers(heades).build().execute(new ObjectCallBack<MessagetBeen>(typeToken) {
            @Override
            public void onError(Call call, Exception e, int id) {
                if(null != loading){
                    loading.setVisibility(View.GONE);
                }

                if(null != recyclerView){
                    recyclerView.stopLoadMore();
                    recyclerView.stopRefresh(true);
                }
            }

            @Override
            public void onResponse(MessagetBeen response, int id) {
                if(isReflush){
                }

                if (null != response) {
                    if (response.getErr_code() == 0) {
                        List<MessagetBeen.MessagesBean.DataBean> DataBeans = response.getMessages().getData();
                        if(null !=DataBeans  && !DataBeans.isEmpty()){
                            data.addAll(DataBeans);
                            ++page;
                        }
                        adapter.reflashData(data);
                    } else {
                        showMsgToast(response.getErr_msg());
                    }
                }

                if(null != loading){
                    loading.setVisibility(View.GONE);
                }

                if(null != recyclerView){
                    recyclerView.stopLoadMore();
                    recyclerView.stopRefresh(true);
                }
            }
        });
    }


    private void read(int id){
        String url = API.MESSAGE+id+"/read?"+"&access_token="+ UserInfoUtil.getInfoToken(getContext());
        HashMap<String, String> heades = new HashMap<>();
        heades.put("Accept", "*/*");

        TypeToken<ModelBeen> typeToken = new TypeToken<ModelBeen>() {
        };

        OkHttpUtils.get().url(url).headers(heades).build().execute(new ObjectCallBack<ModelBeen>(typeToken) {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(ModelBeen response, int id) {


            }
        });
    }
}
